package coupon_distribution

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	"bp/internal/service/entity/coupon"
	"bp/internal/util"
	couponDistributionGrpc "bp/rpc/gen/webapi/coupon_distribution"
	"context"
)

// 新建优惠券投放
func (s *CouponDistributionWebApiImp) CreateCouponDistribution(ctx context.Context, req *couponDistributionGrpc.CreateCouponDistributionReq) (res *couponDistributionGrpc.CreateCouponDistributionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_distribution_imp/cmd.CreateCouponDistribution=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponDistributionGrpc.CreateCouponDistributionRes{}

	var couponDistributionPtr *sqlc.CouponDistribution
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		createCouponDistributionParamPtr, needExecuteImmediately, err := s.VldCreateCouponDistributionReq(ctx, q, req)
		if err != nil {
			return
		}
		couponDistributionPtr, err = s.entityHub.CouponService.CreateCouponDistribution(ctx, q, createCouponDistributionParamPtr)
		if err != nil {
			return
		}
		_, err = s.entityHub.CouponService.EnsureCouponDistributionStat(ctx, q, false, &coupon.EnsureCouponDistributionStatParam{
			CouponDistributionPtr: couponDistributionPtr,
		})
		if err != nil {
			return
		}
		if needExecuteImmediately {
			couponDistributionPtr, err = s.entityHub.CouponService.ExecuteCouponDistribution(ctx, q, &coupon.ExecuteCouponDistributionParam{
				CouponDistributionPtr: couponDistributionPtr,
			})
			if err != nil {
				return
			}
		}
		slog.Infof("新建优惠券投放 ok")
		return
	})
	if err != nil {
		return
	}

	res.CouponDistribution = s.ConvCouponDistributionPtrEntityToPb()(couponDistributionPtr)
	return
}

// 修改优惠券投放
func (s *CouponDistributionWebApiImp) UpdateCouponDistribution(ctx context.Context, req *couponDistributionGrpc.UpdateCouponDistributionReq) (res *couponDistributionGrpc.UpdateCouponDistributionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_distribution_imp/cmd.UpdateCouponDistribution=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponDistributionGrpc.UpdateCouponDistributionRes{}

	var couponDistributionPtr *sqlc.CouponDistribution
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		updateCouponDistributionParamPtr, err := s.VldUpdateCouponDistributionReq(ctx, q, req)
		if err != nil {
			return
		}
		couponDistributionPtr, err = s.entityHub.CouponService.UpdateCouponDistribution(ctx, q, updateCouponDistributionParamPtr)
		if err != nil {
			return
		}
		slog.Infof("更新优惠券投放 ok")
		return
	})
	if err != nil {
		return
	}

	res.CouponDistribution = s.ConvCouponDistributionPtrEntityToPb()(couponDistributionPtr)
	return
}

// 执行优惠券投放
func (s *CouponDistributionWebApiImp) ExecuteCouponDistribution(ctx context.Context, req *couponDistributionGrpc.ExecuteCouponDistributionReq) (res *couponDistributionGrpc.ExecuteCouponDistributionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("coupon_distribution_imp/cmd.ExecuteCouponDistribution=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &couponDistributionGrpc.ExecuteCouponDistributionRes{}

	var couponDistributionPtr *sqlc.CouponDistribution
	err = s.dao.NewTxRunner().SqlcExecTx(ctx, func(q *sqlc.Queries) (err error) {
		executeCouponDistributionParamPtr, err := s.VldExecuteCouponDistributionReq(ctx, q, req)
		if err != nil {
			return
		}
		couponDistributionPtr, err = s.entityHub.CouponService.ExecuteCouponDistribution(ctx, q, executeCouponDistributionParamPtr)
		if err != nil {
			return
		}
		slog.Infof("开始执行优惠券投放 ok")
		return
	})
	if err != nil {
		return
	}

	res.IsExecuting = couponDistributionPtr.IsExecuting
	return
}
